<?php
	include_once 'json_function.php';
	
	$ini_array = parse_ini_file("config/config.ini");

	$conn = @mysqli_connect($ini_array["host"], $ini_array["username"], $ini_array["password"], $ini_array["db_name"]) or die("cann't connect the mysql db");
	mysqli_set_charset($conn, "utf8");
	ini_set('default_charset', "UTF-8");
	date_default_timezone_set($ini_array["default_timezone"]);
	
	$doctorNo = mysqli_real_escape_string($conn, $_POST["doctorNo"]);
	$firstName = mysqli_real_escape_string($conn, $_POST["firstName"]);
	$surname = mysqli_real_escape_string($conn, $_POST["surname"]);
	$clinicNo = mysqli_real_escape_string($conn, $_POST["clinicNo"]);
	
	$getDoctorSql = "select Doctor_no, doctor_first_name, doctor_surname, doctor_address, Clinic_name from doctor left outer join clinic on doctor.clinic_no = clinic.Clinic_no";
	
	$hasParameter = false;
	if( strcmp($doctorNo, "0") != 0 ){
		if( $hasParameter == false ){
			$getDoctorSql .= " where ";
		}else{
			$getDoctorSql .= " and ";
		}
		$getDoctorSql .= " Doctor_no=$doctorNo";
		$hasParameter = true;
	}
	
	if( strcmp($firstName, "0") != 0 ){
		if( $hasParameter == false ){
			$getDoctorSql .= " where ";
		}else{
			$getDoctorSql .= " and ";
		}
		$getDoctorSql .= " doctor_first_name like '%$firstName%'";
		$hasParameter = true;
	}
	
	if( strcmp($surname, "0") != 0 ){
		if( $hasParameter == false ){
			$getDoctorSql .= " where ";
		}else{
			$getDoctorSql .= " and ";
		}
		$getDoctorSql .= " doctor_surname like '%$surname%'";
		$hasParameter = true;
	}
	
	if( strcmp($clinicNo, "0") != 0 ){
		if( $hasParameter == false ){
			$getDoctorSql .= " where ";
		}else{
			$getDoctorSql .= " and ";
		}
		$getDoctorSql .= " doctor.clinic_no=$clinicNo";
		$hasParameter = true;
	}
	$doctorResult = mysqli_query($conn, $getDoctorSql);
	//echo $getDoctorSql;
	$data = "{\"data\":[";
	$index = 0;
	
	while( $row = mysqli_fetch_array($doctorResult)){
		if( $index == 0 ){
			$index++;
		}else{
			$data .= ",";
		}
		$data .= "{";
		$data .= "\"doctorNo\":\"".replaceQuoteAndSlash($row["Doctor_no"])."\",";
		$data .= "\"firstName\":\"".replaceQuoteAndSlash($row["doctor_first_name"])."\",";
		$data .= "\"surname\":\"".replaceQuoteAndSlash($row["doctor_surname"])."\",";
		$data .= "\"address\":\"".replaceQuoteAndSlash($row["doctor_address"])."\",";
		$data .= "\"clinicName\":\"".replaceQuoteAndSlash($row["Clinic_name"])."\"";
		$data .= "}";
	}
	
	$data .= "]}";
	echo JsonCharFilter($data);
	
	
	
?>